package com.right.health.app.ui.me;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.text.TextUtils;
import android.view.View;
import android.widget.ListView;

import com.magic.basic.widget.refresh.OnLastItemVisibleListener;
import com.magic.basic.widget.refresh.SwipeListRefreshLayout;
import com.magics.http.listener.OnHttpListener;
import com.magics.http.model.Request;
import com.magics.http.model.Response;
import com.right.health.app.R;
import com.right.health.app.app.App;
import com.right.health.app.base.BasePresenterActivity;
import com.right.health.app.bean.OrderResponse;
import com.right.health.app.bean.PayInfo;
import com.right.health.app.dialog.BaseDialog;
import com.right.health.app.dialog.ConfirmDialog;
import com.right.health.app.dialog.PayDialog;
import com.right.health.app.net.OrderApi;
import com.right.health.app.ui.me.adapter.MeReserveAdapter;
import com.vendor.social.PayApi;
import com.vendor.social.model.PayType;
import com.vendor.social.pay.WxPay;
import com.vendor.social.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

/*
 * Created by hopeliao on 2019-09-27
 *
 */
public class MeReserveActivity extends BasePresenterActivity implements View.OnClickListener, OnHttpListener, OnLastItemVisibleListener,
        SwipeRefreshLayout.OnRefreshListener,PayApi.OnPayListener {
    private static final int HTTP_CODE_REFRESH = 1;
    private static final int HTTP_CODE_LOAD_MORE = 2;
    private static final int HTTP_CODE_CANCEL = 3;
    private static final int HTTP_CODE_PAY = 4;

    private MeReserveAdapter mAdapter;

    private SwipeListRefreshLayout mRefreshView;

    private int mPageNo;

    private OrderApi mOrderApi;

    private List<OrderResponse.OrderInfo> mDataSource = new ArrayList<>();

    private ConfirmDialog mCancelDialog;

    private PayDialog mPayDialog;

    private int mPayPosition;

    @Override
    public void onCreate(Bundle bundle, String s) {
        setContentView(R.layout.me_reserve);
    }

    @Override
    public void findView() {

        mRefreshView = (SwipeListRefreshLayout) findViewById(R.id.refresh_view);
        mRefreshView.setLastItemVisibleListener(this);
        mRefreshView.setOnRefreshListener(this);

        ListView listview = (ListView) findViewById(R.id.listview);
        mAdapter = new MeReserveAdapter(this);
        mAdapter.setOnClickListener(this);
        listview.setAdapter(mAdapter);

    }

    @Override
    public void initialize() {
        mOrderApi = new OrderApi();
        mOrderApi.setListener(this);

        mRefreshView.setRefreshing(true);
        onRefresh();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_cancel:
                int pos = Integer.parseInt(String.valueOf(v.getTag()));

                if (mCancelDialog == null) {
                    mCancelDialog = new ConfirmDialog(this);
                    mCancelDialog.setDelegate(new BaseDialog.IDialogDelegate() {
                        @Override
                        public void onDialogCallback(BaseDialog dialog) {
                            dialog.dismiss();

                            OrderResponse.OrderInfo orderInfo = mDataSource.get(pos);

                            mOrderApi.addRequestCode(HTTP_CODE_CANCEL);
                            mOrderApi.setTag(pos);
                            mOrderApi.cancelOrder(orderInfo.orderid);

                        }
                    });
                }
                mCancelDialog.show();
                break;
            case R.id.btn_modify:
                if(mPayDialog == null) {
                    mPayDialog = new PayDialog(this);
                    mPayDialog.setClickListener(this);
                }
                mPayPosition = Integer.parseInt(String.valueOf(v.getTag()));
                mPayDialog.show();
                break;
            case R.id.tv_pay_alipay:

                toPay(11);
                break;
            case R.id.tv_pay_weixin:
                toPay(10);
                break;
        }
    }

    private void toPay(int type) {
        dismissPayDialog();
        OrderResponse.OrderInfo info = mDataSource.get(mPayPosition);

        mOrderApi.addRequestCode(HTTP_CODE_PAY);
        mOrderApi.toPay(info.orderid, String.valueOf(type));

    }

    private void dismissPayDialog() {
        if(mPayDialog != null && mPayDialog.isShowing()) {
            mPayDialog.dismiss();
        }
    }

    private void dismissCancelDialog() {
        if (mCancelDialog != null && mCancelDialog.isShowing()) {
            mCancelDialog.dismiss();
        }
    }

    @Override
    public void onRefresh() {
        mPageNo = 1;
        mOrderApi.addRequestCode(HTTP_CODE_REFRESH);
        mOrderApi.getOrder(mPageNo, App.getInstance().getUserId());
    }

    @Override
    public void onLastItemVisible() {
        mPageNo += 1;
        mOrderApi.addRequestCode(HTTP_CODE_LOAD_MORE);
        mOrderApi.getOrder(mPageNo, App.getInstance().getUserId());
    }

    @Override
    public void onResponse(Request request, Response response) {
        mRefreshView.onRefreshComplete();
        if (response.isResponseFail()) {
            ToastUtil.show(this, response.error);
        } else {
            if (request.requestCode == HTTP_CODE_CANCEL) {
                int pos = Integer.parseInt(String.valueOf(request.tag));
                OrderResponse.OrderInfo orderInfo = mDataSource.get(pos);
                orderInfo.orderstatus = OrderResponse.ORDER_STATE_CANCEL;
                mDataSource.set(pos, orderInfo);

                mAdapter.setDataSource(mDataSource);
            } else if(request.requestCode == HTTP_CODE_PAY) {
                if(response.targetData instanceof PayInfo) {
                    PayInfo payInfo = response.cast(PayInfo.class);

                    if(!TextUtils.isEmpty(payInfo.orderstr)) {
                        PayApi.pay(this, PayType.ALIPAY, payInfo.orderstr, this);
                    } else if (payInfo.wxdata!=null) {
                        WxPay wxPay = new WxPay(this);
                        wxPay.setOnPayListener(this);
                        wxPay.pay(payInfo.wxdata);
                    }

                }
            }
            if (response.targetData instanceof OrderResponse) {
                OrderResponse orderResponse = response.cast(OrderResponse.class);
                if (request.requestCode == HTTP_CODE_REFRESH) {
                    mDataSource.clear();
                }
                mDataSource.addAll(orderResponse.orders);

                if (orderResponse.page != null) {
                    mRefreshView.setCanLoadMore(!orderResponse.page.lastPage);
                }
                mAdapter.setDataSource(mDataSource);
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        dismissCancelDialog();
        dismissPayDialog();
    }

    @Override
    public void onPayOk() {
        ToastUtil.show(this, "支付成功");

        mRefreshView.setRefreshing(true);
        onRefresh();
    }

    @Override
    public void onPayFail(String msg) {
        ToastUtil.show(this, msg);
    }
}
